package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Order;
import com.ericom.service.GenericManager;

public class OrderAction extends BaseAction {
	private GenericManager<Order, Long> orderManager;
	private List orders;
	private Order order;
	private Long id;

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setOrderManager(GenericManager<Order, Long> orderManager) {
		this.orderManager = orderManager;
	}

	public String list() {
		orders = orderManager.getAll();
		return SUCCESS;
	}

	public List getOrders() {
		return orders;
	}

	public String delete() {
		orderManager.remove(order.getId());
		this.saveMessage(getText("order.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			order = orderManager.get(id);
		} else {
			order = new Order();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (order.getId() == null);
	
		order = orderManager.save(order);

		String key = (isNew) ? "order.added" : "order.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
